package org.grow.share;

import org.grow.share.filter.CorsTestFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.RegistrationBean;
import org.springframework.context.annotation.Bean;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;

@SpringBootApplication
public class ShareApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(ShareApplication.class, args);
    }
    @Autowired
    private CorsTestFilter corsTestFilter;
    @Bean
    public FilterRegistrationBean corsFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setOrder(0);
        registrationBean.setFilter(corsTestFilter);
        registrationBean.addUrlPatterns("*");
        return registrationBean;
    }
    @Bean
    public Random random(){
        return new Random();
    }
    @Value("${target}")
    private String downloadDir;
    @Value("${repo}")
    private String uploadDir;
    @Override
    public void run(String... args) throws Exception {
        Path dd = Paths.get(downloadDir);
        Path ud = Paths.get(uploadDir);
        if (!Files.exists(dd)){
            Files.createDirectory(dd);
        }
        if (!Files.exists(ud)){
            Files.createDirectory(ud);
        }
    }
}
